﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Zyg.SSO.Core
{
    [Serializable()]
    public class APUserInfo
    {
        #region Fields and Properties

        private string username = string.Empty;
        /// <summary>
        /// 用户ming
        /// </summary>
        public string UserName { get { return username; } set { username = value; } }

        private string password = string.Empty;
        /// <summary>
        /// yonghumima
        /// </summary>
        public string PassWord { get { return this.password; } set { password = value; } }

        

        #endregion

        #region Static Methods

        public static string Serialize(APUserInfo userinfo)
        {
            MemoryStream stream = new MemoryStream();

            IFormatter formatter = new BinaryFormatter();

            formatter.Serialize(stream, userinfo);

            byte[] buffer = stream.ToArray();

            return Convert.ToBase64String(buffer);
        }

        public static APUserInfo DeSerialize(string serializedString)
        {
            try
            {
                byte[] buffer = Convert.FromBase64String(serializedString);

                MemoryStream stream = new MemoryStream(buffer);

                IFormatter formatter = new BinaryFormatter();

                APUserInfo userinfo = formatter.Deserialize(stream) as APUserInfo;
                return userinfo;
            }
            catch
            {
                throw new Exception("用户信息转化失败");
            }
        }

        #endregion
    }
}
